/**
 * 
 */
package br.unesp.ibb.lbbc.fba;

import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.SwingWorker;
import javax.xml.stream.XMLStreamException;

import org.sbml.jsbml.ListOf;
import org.sbml.jsbml.Model;
import org.sbml.jsbml.Reaction;
import org.sbml.jsbml.SBMLDocument;
import org.sbml.jsbml.SBMLReader;
import org.sbml.jsbml.Species;

/**
 * @author Esther
 *
 */
public class StoichometryTask extends SwingWorker<double[][], Void> {

	

	private static double[][] matrix;
		/*
		 * Main task. Executed in background thread.
		 */
		
		@Override
		//devolve matriz
		public double[][] doInBackground() {

			int progress = 0;
			setProgress(0);

			File sbmlFile = new File(
					"D:\\0-DOUTORADO\\TRABALHO\\PLUGIN-FBA\\cyplugin\\ecoli.xml");
			SBMLReader reader = new SBMLReader();
			SBMLDocument doc = null;
			try {
				doc = reader.readSBML(sbmlFile);
			} catch (XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			Model model = doc.getModel();
			ListOf<Reaction> listReactions = model.getListOfReactions();
			ListOf<Species> listMetabolites = model.getListOfSpecies();

			double [][] matrix = new double[listReactions.size()][listMetabolites.size()];
			matrix[0][0]=6;
			
			int numMetabolitos = listMetabolites.size();

			for (Species met : listMetabolites) {
				progress = progress + 1;
				setProgress(progress * 100 / numMetabolitos);

				int indexM = listMetabolites.indexOf(met);

		/*		for (Reaction reaction : listReactions) {

					int indexR =listReactions.indexOf(reaction);
					
					ListOf<SpeciesReference> reactants = reaction.getListOfReactants();
					for (SpeciesReference ref : reactants) {
						matrix[indexR][indexM] = (-1) * ref.getStoichiometry();
					}

					ListOf<SpeciesReference> products = reaction.getListOfProducts();
					for (SpeciesReference ref : products) {
						matrix[indexR][indexM] = ref.getStoichiometry();
					}

				}*/
			}
			
			ProgressBar.frame.dispatchEvent(new WindowEvent(ProgressBar.frame, WindowEvent.WINDOW_CLOSING)); 

			return matrix;
		}

		public static double[][] getMatrix(){
			return matrix;
		}
	
	
}
